<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="用户id" prop="userId">
                <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="服务人员姓名" prop="staffName">
                <el-input v-model="queryParams.staffName" placeholder="请输入服务人员姓名" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="服务人员电话" prop="staffPhone">
                <el-input v-model="queryParams.staffPhone" placeholder="请输入服务人员电话" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['miniprogram:staff:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['miniprogram:staff:edit']">修改</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="staffList" @expand-change="rowExpand" :row-key="getRowKey"
            :expand-row-keys="expands" @selection-change="handleSelectionChange">
            <el-table-column type="expand" prop="">
                <template slot-scope="scope">
                    <el-table :data="staffOrderList">
                        <el-table-column align="center" label="订单服务id">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrderUser.orderUserId }}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="订单编号">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrder.orderNumber }}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="服务项">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrder.serviceName }}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="联系人">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrderUser.receiver }}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="联系电话">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrderUser.receiverPhone }}
                            </template>
                        </el-table-column>
                        <el-table-column align="center" label="服务时间">
                            <template slot-scope=" scope">
                                {{ scope.row.miniProgramOrderUser.serviceTime }}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                            <template slot-scope="scope">
                                <el-button v-show="scope.row.miniProgramServiceLog.workStatus == 0" size="mini" type="text"
                                    icon="el-icon-delete" @click="handleDeleteOrder(scope.row)">取消</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </template>
            </el-table-column>
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="id" align="center" prop="staffId" width="100" />
            <el-table-column label="用户id" align="center" prop="userId" />
            <el-table-column label="服务人员姓名" align="center" prop="staffName" />
            <el-table-column label="服务人员电话" align="center" prop="staffPhone" />
            <el-table-column label="服务人员状态" align="center" width="100">
                <template slot-scope="scope">
                    <div class="staffStatus" :style="'color:' + staffStatus[scope.row.staffStatus].color">{{
                        staffStatus[scope.row.staffStatus].name }}</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改服务工人对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="用户id" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="服务人员姓名" prop="staffName">
                    <el-input v-model="form.staffName" placeholder="请输入服务人员姓名" />
                </el-form-item>
                <el-form-item label="服务人员电话" prop="staffPhone">
                    <el-input v-model="form.staffPhone" placeholder="请输入服务人员电话" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
  
<script>
import { listStaff, staffOrderList, deleteStaffOrder, getStaff, addStaff, updateStaff } from "@/api/miniprogram/staff";

export default {
    name: "Staff",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 服务工人表格数据
            staffList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userId: null,
                staffStatus: null,
                staffName: null,
                staffPhone: null
            },
            //当前展开行
            expands: [],
            //被分配服务列表
            staffOrderList: [],
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            },
            staffStatus: [
                {
                    name: "未分配",
                    color: "green"
                },
                {
                    name: "已分配",
                    color: "blue"
                },
                {
                    name: "离职/休假",
                    color: "grey"
                }
            ]
        };
    },
    created() {
        this.getList();
    },
    methods: {
        getRowKey(row) {
            return row.userId;
        },
        /** 查询服务工人列表 */
        getList() {
            this.loading = true;
            listStaff(this.queryParams).then(response => {
                this.staffList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        //列表展开，获取被分配订单
        rowExpand(row, expandRows) {
            if (expandRows.length) {
                this.expands = [];
                this.staffOrderList = [];
                if (row) {
                    this.expands.push(row.userId);
                    this.getStaffOrder(row.userId);
                }
            }
        },
        //根据userId获取订单分配信息
        getStaffOrder(userId) {
            this.loading = true;
            staffOrderList(userId).then(response => {
                this.staffOrderList = response.data;
                this.loading = false;
            });
        },
        //取消被分配订单
        handleDeleteOrder(row) {
            console.log(row);
            this.$modal.confirm('确认取消分配？').then(() => {
                this.loading = true;
                deleteStaffOrder(row.miniProgramServiceLog.serviceLogId).then(response => {
                    if (response.code == 200) {
                        this.$modal.msgSuccess("操作成功");
                        this.getStaffOrder(this.expands[0]);
                        this.getList();
                    }
                });
            }).catch(() => { });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                staffId: null,
                userId: null,
                staffStatus: null,
                staffName: null,
                staffPhone: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.staffId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加服务工人";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const staffId = row.staffId || this.ids
            getStaff(staffId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改服务工人";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.staffId != null) {
                        updateStaff(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addStaff(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
    }
};
</script>
<style>
.staffStatus {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 40px;
    width: 80px;
    border-radius: 5px;
}
</style>